package com.cn.test.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PrefShare {
    private SharedPreferences preferences;
    private Context context;
    private static PrefShare prefShare;

    private PrefShare(Context context) {
        this.context = context;
        preferences = PreferenceManager
                .getDefaultSharedPreferences(this.context);
    }

    public static PrefShare getInstance(Context context) {
        if (prefShare == null) {
            synchronized (PrefShare.class) {
                if (prefShare == null)
                    prefShare = new PrefShare(context);
            }
        }
        return prefShare;
    }

    //保存String
    public synchronized boolean saveString(String key, String value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    //取出String
    public synchronized String getString(String key) {
        return preferences.getString(key, "");
    }

    //保存Int
    public synchronized boolean saveInt(String key, int value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    //取出Int
    public synchronized int getInt(String key) {
        return preferences.getInt(key, 0);
    }

    //保存Boolean
    public synchronized boolean putIsBoolean(Context context, String key, boolean value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    //取出boolean
    public synchronized boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences.Editor editor = preferences.edit();
        return preferences.getBoolean(key, defValue);
    }

    //保存Double
    public synchronized boolean saveDouble(String key, double value) {
        return saveString(key, String.valueOf(value));
    }

    public synchronized double getDouble(String key) {
        String value = getString(key);
        if (null != value) {
            return Double.valueOf(value);
        } else {
            return 0;
        }
    }

//
//    //保存对象  对象是已jsonstring的形式存储
//    public synchronized boolean saveLoginBean(LoginRegisterResult.UserinfoBean value) {
//        //将存的对象  转化为jsonstring
//        String jsonString = JsonUitl.objectToString(value);
//        SharedPreferences.Editor editor = preferences.edit();
//        editor.putString("loginInfo", jsonString);
//        return editor.commit();
//    }
//
//    //取出对象
//    public synchronized LoginRegisterResult.UserinfoBean getLoginBean() {
//        String loginInfo = preferences.getString("loginInfo", null);
//        return (LoginRegisterResult.UserinfoBean) JsonUitl.stringToObject(loginInfo, LoginRegisterResult.UserinfoBean.class);
//    }


}
